package pri.ydy.eduService.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import pri.ydy.commonUtils.R;
import pri.ydy.eduService.config.vo.TeacherQuery;
import pri.ydy.eduService.entity.EduTeacher;
import pri.ydy.eduService.service.EduTeacherService;

import java.util.List;


/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ydy
 * @since 2020-09-24
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/edu-teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    //查询所有
    @ApiOperation(value = "所有讲师列表")//swagger注解
    @GetMapping("findAll")
    public R findAll() {
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }

    //逻辑删除讲师
    @ApiOperation(value = "讲师的逻辑删除")//swagger注解
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id) {//获得路径中的id值
        boolean flag = teacherService.removeById(id);
        if (flag) return R.ok();
        else return R.error();
    }

    //分页查询讲师方法
    //current 当前页
    //limit 每页记录数
    @ApiOperation(value = "讲师的分页查询")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current, @PathVariable long limit) {

        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher中去
        teacherService.page(pageTeacher, null);//第二个是条件
        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords();//数据集合
        return R.ok().data("total", total).data("rows", records);

        //第二种方式
       /* Map map =new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);*/
    }

    //讲师的多条件查询
    @ApiOperation(value = "讲师的多条件分页查询")
    @PostMapping("TeacherQuery/{current}/{limit}")
    public R TeacherQuery(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) TeacherQuery teacherquery) { //RequestBody 接收json格式的数据  只能post请求
        //创建一个page对象
        Page<EduTeacher> page = new Page<>(current, limit);

        //构建wrapper条件对象
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询
        //mybatis学过 动态SQL
        String name = teacherquery.getName();
        Integer level = teacherquery.getLevel();
        String begin = teacherquery.getBegin();
        String end = teacherquery.getEnd();
        //判断条件之是否为空，如果不为空
        if (!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_modified", end);
        }

        //排序
        wrapper.orderByDesc("gmt_create");


        //调用方法实现分页
        teacherService.page(page, wrapper);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }


    //添加讲师接口的方法
    @ApiOperation(value = "添加讲师接口的方法")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher teacher) {
        boolean save = teacherService.save(teacher);
        if (save) {
            return R.ok();
        } else return R.error();
    }

    //根据讲师id查询
    @ApiOperation(value = "根据讲师id查询")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id) {
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }

    //根据teacher对象进行修改
    @ApiOperation(value = "根据teacher对象进行修改")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.updateById(eduTeacher);
        if (save) {
            return R.ok();
        } else return R.error();
    }


}

